package com.winderinfo.yikaotianxia.ui.kuaijieLogin;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.MainActivity;
import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.login.LoginActivity;
import com.winderinfo.yikaotianxia.ui.register.DuanxinBean;
import com.winderinfo.yikaotianxia.ui.register.RegisterBean;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class KuaiJieLoginActivity extends BaseActivity implements Contract.View {
    @BindView(R.id.qu_phone)
    EditText quPhone;
    @BindView(R.id.qu_code)
    EditText quCode;
    @BindView(R.id.qu_getcode)
    TextView quGetcode;
    @BindView(R.id.qu_login)
    Button quLogin;
    @BindView(R.id.qu_tlogin)
    TextView quTlogin;

    private int time = 60;
    private boolean flag = true;
    //获取到的短信
    private String getCode = "";
    //创建Map
    Map<String, Object> map = new HashMap<>();

    private Presenter presenter;
    private String phone;
    private String code;


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    flag = false;
                    time--;
                    //regGetcode.setTextColor(0xFB7123);
                    quGetcode.setText(time + "  S");
                    if (time > 0) {
                        Message message = handler.obtainMessage(1);
                        handler.sendMessageDelayed(message, 1000);
                    } else {
                        time = 60;
                        flag = true;
                        quGetcode.setText("获取验证码");
                    }
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected int initLayoutId() {
        return R.layout.activity_quicklogin;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        //实例P
        presenter = new Presenter(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.qu_getcode, R.id.qu_login, R.id.qu_tlogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.qu_getcode:
                //获取输入框信息
                phone = quPhone.getText().toString();
                //判断手机号是否为空
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showSuccess(this, "请输入手机号");
                    return;
                }
                //搬到是否可以点击
                if (flag) {
                    Message message = handler.obtainMessage(1);
                    handler.sendMessageDelayed(message, 1000);
                    //发送短信请求
                    /**
                     * mobile  手机号
                     * */
                    map.clear();
                    map.put("mobile", phone);
                    presenter.onPostRequest(Api.DUANXIN, map, DuanxinBean.class);
                }
                break;
            case R.id.qu_login:
                //获取输入框信息
                phone = quPhone.getText().toString();
                code = quCode.getText().toString();
                //判断手机号是否为空
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showSuccess(this, "请输入手机号");
                    return;
                }
                //判断短信
                if (!code.equals(getCode)) {
                    ToastUtil.showWarning(this, "验证码错误");
                    return;
                }
                //请求登录
                /**
                 *@Phone 手机号
                 * */
                map.clear();
                map.put("phone", phone);
                presenter.onPostRequest(Api.KUAIJIELOGIN, map, LoginBean.class);
                break;
            case R.id.qu_tlogin:
                openActivityAndCloseThis(LoginActivity.class);
                break;
        }
    }

    @Override
    public void onSuccess(Object data) {
        //短信
        if (data instanceof DuanxinBean) {
            this.getCode = ((DuanxinBean) data).getMsg();
        }
        //登录
        if (data instanceof LoginBean) {
            //登录失败
            if (((LoginBean) data).getCode() == 500) {
                ToastUtil.showSuccess(this, ((LoginBean) data).getMsg());
            }
            //登录成功
            if (((LoginBean) data).getCode() == 0) {
                SPUtils spUtils = new SPUtils(this);
                spUtils.putString("UserID",((LoginBean) data).getUserId()+"");
                ToastUtil.showSuccess(this, ((LoginBean) data).getMsg());
                openActivityAndCloseThis(MainActivity.class);
            }
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }
}
